<%@ page language="java" contentType="text/html; charset=UTF-8"  pageEncoding="UTF-8"%>
<!DOCTYPE HTML>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<title>游戏区管理</title>
	<%@ include file="/view/common/tags.jsp"%>
	<script type="text/javascript" src="${sysManagerV1}/js/My97DatePicker/WdatePicker.js"> </script>
</head>
<body>
	<section class="content">
    	<div class="row">
            <div class="col-md-12">
                <div class="box">
                	<div class="box-body">
						<div class="panel panel-default">
                            <div class="panel-body" style="padding-bottom:5px;">
                              <form class="form-horizontal" action="" method="post" id="dataForm">
                                  <input type="hidden" id="token" name="token" value="">
	                              <input id="gameId" name="wm[GAME_ID]" type="hidden" value="" >
		                          <input id="areaId" name="wm[AREA_ID]" type="hidden" value="<%=request.getParameter("areaId") %>" >
    	                          <div class="searchMenu">
    	                             <div> 
    	                               <label>游戏名称：</label>
    	                               <span class="textContent" id="gameName">${tlzsGame.gameName}</span>
    	                             </div>
    								 <div>
    								   <label>游戏区名称：</label>
    								   <span class="textContent" id="areaName">${tlzsGameArea.areaName}</span>
    								 </div>
                                     <div>
    	                                <label>服务器名称：</label>
    	                                <input type="text" class="form-control" name="wm[_LIKE$SERVER_NAME]" value="${page.wm._LIKE$SERVER_NAME}"  placeholder="请输入服务器名称">
    	                             </div>
    	                             <div>
    	                                <label>选择状态：</label>
    	                                    <sk:select cssClass="form-control select" name="wm[USE_FLAG]" value="${page.wm.USE_FLAG}" >
    	                                    	<option value="">请选择</option>
    	                                    	<option value="0">停用</option>
    	                                    	<option value="1">启用</option>
    	                                    </sk:select>
    	                                </div>
    	                                <div class="mal10" >
    	                                	<button type="button"  class="btn btn-primary btn-sm"  onclick="goBack()"><span class='glyphicon glyphicon-backward'></span>返&nbsp;回</button>
    	                                	<button type="button"  class="btn btn-primary btn-sm"  onclick="findData()"><span class='glyphicon glyphicon-search'></span>查&nbsp;询</button>
    	                               		<button type="button"  class="btn btn-success btn-sm"  onclick="add()"><span class='glyphicon glyphicon-plus'></span>添&nbsp;加</button>
    	                            	</div>
    	                            </div>
                                </form>
                        	</div>
						</div>
                        <table class="table table-hover table-condensed table-bordered table-striped" id="dataTable" >
                            <thead>
                            	<tr>
                                	<th data-field="index" data-align="center" data-formatter="indexFormatter" data-sortable="false" >序号</th>
                                	<th data-field="rowId" data-align="center" data-sortable="true">服务器ID</th>
                                	<th data-field="serverName" data-align="center" data-sortable="true">服务器名称</th>
                                	<th data-field="orderNum" data-align="center" data-sortable="true">排序号</th>
                                    <th data-field="useFlag" data-align="center" data-formatter="useFlagFormatter" data-sortable="true">状态</th>
                                    <th data-field="optAccount" data-align="center" data-sortable="true">操作人帐号</th>
                                    <th data-field="optTime" data-align="center" data-formatter="timeFormatter_point" data-sortable="true" >操作时间</th>
                                    <th data-field="deleteButton" data-align="center"  data-formatter="buttonFormatter" data-sortable="false">操作</th>
                                </tr>
                            </thead>
                        </table>
                    </div>
                </div>
            </div>
       	</div>
	</section>
   <%@ include file="/view/common/footer.jsp"%>
</body>
<script>
	// 提示信息
	var msgg='<%=request.getAttribute("msg")%>';
	if (msgg && msgg!=null && msgg!='null') { 
		  layer.msg(msgg);
	}
	var tableIndex = 0; // 添加修改的弹出框的ID
	
	$(function(){
		$("#token").val(sessionStorage.getItem("token"));
		
		// 获取游戏信息
		var areaId = $("#areaId").val();
        $.ajax({
            url: "${userInfoV1}/baseArea/getInfo",
            type: "post",
            cache: false,
            data:{"rowId":areaId},
            dataType:"json",
            async:false,
            success: function(data) {
                if (data == null) {
                    return;
                }
                $("#gameId").val(data.gameId);
                $("#gameName").html(data.gameName);
                $("#areaName").html(data.areaName);
            }
        });
        
        // 加载数据
        $("#dataTable").bootstrapTable({ 
            url: "${userInfoV1}/baseServer/data.json",
            method:"get", 
            pagination:true,
            sidePagination:"server",
            queryParams: function(params) {
                return $.extend({}, params, parseJson(decodeURIComponent($("#dataForm").serialize())));     
            }
       }); 
	})

    // 点击行事件
    $(function(){
         $("body").on("click","table>tbody>tr",function(){
          $(this).css("background","#e1ecf1").siblings().css("background","");
       });
    })
    
    //查询
    function findData(){
       $("#dataTable").bootstrapTable("refresh",{} ); 
    } 
	
	//返回维护区服页面
	function goBack(){
	    window.location.href="${sysManagerV1}/gameArea/list";
	}
	
    // 启用/停用
    function useFlagFormatter(value, row) {
        var html = "";
        if (row.useFlag == 1) {
            html = "<span class='text-success'>启用 </span>&nbsp;&nbsp;&nbsp;&nbsp;";
            html += "<button id='btn_edit' type='button' onclick='_stop("+row.rowId+", "+row.useFlag+", \""+row.areaName+"\");'    class='btn btn-default btn-sm '><span class='glyphicon glyphicon-stop'></span>停用</button>";
        } else {
            html = "<span class='text-warning'>停用 </span>&nbsp;&nbsp;&nbsp;&nbsp;";
            html += "<button id='btn_edit' type='button' onclick='_stop("+row.rowId+", "+row.useFlag+", \""+row.areaName+"\");'    class='btn btn-default btn-sm '><span class='glyphicon glyphicon-ok'></span>启用</button>";
        }
        return html;
    }
    
    // 操作按钮
    function buttonFormatter(value, row, index) {
        var updateArea = "<button id='btn_edit' type='button' onclick='_update("+row.rowId+");'  class='btn btn-default btn-sm'><span class='glyphicon glyphicon-pencil'></span>修 改</button>";
        var deleteArea = "<button id='btn_edit' type='button' onclick='deleteArea("+row.rowId+", "+row.useFlag+");' class='btn btn-default btn-sm'><span class='glyphicon glyphicon-remove'></span>删 除</button>";
        return updateArea + deleteArea;
    }

    // 弹出添加的窗口
    function add(){
        tableIndex = layer.open({
            type: 2,
            title: "添加游戏区",
            closeBtn: 1,
            shadeClose: true,
            area: ['380px', '265px'],
            content: "${sysManagerV1}/gameServer/toAddGameServer?areaId=" + $("#areaId").val() + "&rowId=0&flag=0",
            btn: ["确 认","关 闭"], 
            yes: function(index, layero){
                var iframeWin = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象，执行iframe页的方法：
                iframeWin.add();
            }
        });
    }
    
    // 弹出添加的窗口      修改功能
    function _update(rowId){
        tableIndex = layer.open({
            type: 2,
            title: "修改游戏区",
            closeBtn: 1,
            shadeClose: true,
            area: ['380px', '265px'],
            content: "${sysManagerV1}/gameServer/toAddGameServer?areaId=" + $("#areaId").val() + "&rowId=" + rowId + "&flag=1",
            btn: ["确 认","关 闭"], 
            yes: function(index, layero){
                var iframeWin = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象，执行iframe页的方法：
                iframeWin.add();
            }
        });
    }
    
    //停用（启用）
    function _stop(rowId, useFlag, areaName){
        //var gameName = getTableColumnValueByDict($("#gameNameListJson").val(), gameId);
        var descStr = '【停用】<a style="color:red">'+'"'+areaName+'"'+'</a>';
        if (useFlag == 0) {
            descStr = '【启用】<a style="color:red">'+'"'+areaName+'"'+'</a>';
        }
        useFlag = Math.abs(Number(useFlag) - 1);
        layer.confirm('您确认'+descStr+'游戏区吗？', {
            btn: ['确认', '取消']  
        }, 
        function(){
            if (rowId == null || rowId == "") {
                layer.msg("id不能为空!");
                return;
            }
            $.ajax({
                url: "${userInfoV1}/baseServer/updateUseState",
                type: "post",
                cache: false,
                data: {rowId:rowId, useFlag:useFlag, token: sessionStorage.getItem("token")},
                dataType:"json",
                async:false,
                success: function(data) {
                    if (data == 1) { 
                        layer.msg(descStr+"成功", {icon: 1});
                        findData();
                    } else if(data == "-2"){
                        window.location.href="${sysManagerV1}/";
                    } else {
                        layer.msg(descStr+"失败", {icon: 5});
                    }
                }
            });
        });
    }

    //删除游戏
    function deleteArea(rowId, useFlag){
        var descStr = "【删除】";
        layer.confirm('您确认'+descStr+'?', {
            btn: ['确认', '取消']  
        },
        function(){
            $.ajax({
                url: "${userInfoV1}/baseServer/delete",
                type: "post",
                cache: false,
                data: {rowId:rowId, useFlag:useFlag, token: sessionStorage.getItem("token")},
                dataType:"json",
                async:false,
                success: function(data) {
                    if (data == 1) { 
                        layer.msg(descStr+"成功", {icon: 1});
                        findData();
                    } else if (data == 3) {
                        layer.msg(descStr+"失败,该游戏启用中");
                    } else if(data == "-2"){
                        window.location.href="${sysManagerV1}/";
                    } else {
                        layer.msg(descStr+"失败", {icon: 5});
                    }
                }
            });
        });
    }
</script>
</html>